package com.wnxy.admin.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;

import java.util.Map;

public class TokenUtil {

    public static String getPayLoad(String token,String key){
        JWT jwt = JWTUtil.parseToken(token);
        return (String) jwt.getPayload(key);
    }

    public static String createToken(Map<String,Object> payload,String signature){
        JWTSigner signer = JWTSignerUtil.hs256(signature.getBytes());
        JWT jwt = JWT.create().setIssuedAt(DateUtil.date());
        payload.entrySet().stream().forEach(e->{
            jwt.setPayload(e.getKey(),e.getValue());
        });
        return jwt.setKey(signature.getBytes()).sign();
    }

    public static String createToken(Map<String,Object> payload,String signature,Integer timeout){
        JWTSigner signer = JWTSignerUtil.hs256(signature.getBytes());
        JWT jwt = JWT.create().setIssuedAt(DateUtil.date());
        payload.entrySet().stream().forEach(e->{
            jwt.setPayload(e.getKey(),e.getValue());
        });
        return jwt.setKey(signature.getBytes()).sign();
    }
}
